﻿package com.ufenlei.LTS {
	import flash.display.Sprite;
	import flash.events.Event;

	/**
	 * @author bbn
	 */
	public class List extends Sprite {
		private var itemlist:Array = new Array();
		public function addItem(item:ListItem):void{
			itemlist.push(item);
			item.addEventListener(Input.CHANGE,onChange);
			refreshView();
		}
		public function removeItem(item:ListItem):void {
			itemlist = itemlist.filter(function(i:ListItem,idx:int,a:Array):Boolean{
				return i != item;
			});
			this.removeChild(item);
			this.refreshView();
		}
		private function onChange(evt:Event):void{
			this.refreshView();
		}
		public function get length():int{
			return this.itemlist.length;
		}

		public function refreshView():void{
			var iy:int = 0;
			this.graphics.clear();
			this.graphics.lineStyle(1,0x999999,1);
			this.graphics.moveTo(0, 0);
			for each(var item:ListItem in itemlist){
				item.x = 10;
				item.y = iy;
				iy += item.height;
				this.addChild(item);
				this.graphics.lineTo(0,item.itemHeight/2+item.y);
				this.graphics.lineTo(10,item.itemHeight/2+item.y);
				this.graphics.moveTo(0,item.itemHeight/2+item.y);
			}
		}
	}
}
